package com.dfec.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.book.dao.BookDao;
import com.dfec.book.entity.BookEntity;
import com.dfec.book.exception.ServiceException;
import com.dfec.book.service.BookCategoryService;
import com.dfec.book.service.BookService;
import com.dfec.book.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 图书服务实现类
 *
 * @author tangrg
 * @email 1446232546@qq.com
 * @date 2025-06-19 10:36:39
 */
@Service
@Transactional
public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements BookService {

    private final BookCategoryService bookCategoryService;

    @Autowired
    public BookServiceImpl(BookCategoryService bookCategoryService) {
        this.bookCategoryService = bookCategoryService;
    }


    @Override
    public PageUtils getBooksByPage(BookEntity bookEntity) {
        Page<BookEntity> page = new Page<>(bookEntity.getPageNo(), bookEntity.getPageSize());
        IPage<BookEntity> bookEntityIPage = baseMapper.selectPage(page, bookEntity);
        return new PageUtils(bookEntityIPage);
    }


    @Override
    public boolean addBook(BookEntity book) {
        // 验证类别是否存在
        if (!bookCategoryService.existsById(book.getCategoryId())) {
            throw new ServiceException("类别ID不存在");
        }

        // 检查图书名称是否唯一 (可选)
        QueryWrapper<BookEntity> query = new QueryWrapper<>();
        query.eq("book_name", book.getBookName());
        if (count(query) > 0) {
            throw new ServiceException("图书名称已存在");
        }
        book.setCreateTime(LocalDateTime.now());
        book.setIsBorrowed(false);
        return save(book);
    }

    @Override
    public boolean updateBook(BookEntity book) {
        if (Objects.isNull(book.getBookId())) {
            throw new ServiceException("图书ID不能为空");
        }
        BookEntity bookInfo = getById(book.getBookId());
        if (bookInfo == null) {
            throw new ServiceException("图书不存在");
        }
        // 验证类别是否存在
        if (!bookCategoryService.existsById(book.getCategoryId())) {
            throw new ServiceException("类别ID不存在");
        }
        book.setUpdateTime(LocalDateTime.now());
        return updateById(book);
    }

    @Override
    public boolean deleteBook(List<Integer> bookIds) {
        // 检查图书是否被借出
        for (Integer bookId : bookIds) {
            BookEntity book = getById(bookId);
            if (book != null && book.getIsBorrowed()) {
                throw new ServiceException("图书已被借出，无法删除");
            }
        }
        return removeByIds(bookIds);
    }

    @Override
    public BookEntity getBookById(Integer bookId) {
        return getById(bookId);
    }


    @Override
    public List<BookEntity> getBooksByCategory(Integer categoryId) {
        BookEntity bookEntity = new BookEntity();
        bookEntity.setCategoryId(categoryId);
        return baseMapper.selectByExample(bookEntity);
    }


    @Override
    public boolean updateBookBorrowStatus(Integer bookId, boolean isBorrowed) {
        BookEntity book = getById(bookId);
        if (book == null) {
            throw new ServiceException("图书不存在");
        }
        book.setIsBorrowed(isBorrowed);

        QueryWrapper<BookEntity> qw = new QueryWrapper<>();
//        qw.eq("book_id", bookId);
        qw.like("book_name","java");
//        LambdaQueryWrapper
        this.baseMapper.selectList(qw);

        return updateById(book);
    }
}